﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CourseArraging.Common;
using System.Text.RegularExpressions;
using Stone.Library.Common; 

namespace CourseArraging.UControl
{
    public partial class UCcurriculumWrite : UCcurriculum
    {
        #region 参数
        private int x_coor;//存储cellchanged的x坐标
        private int y_coor;//存储cellchanged的y坐标
        private string course_cell_data;//存储时间格式1-2-15:30-17:30
        private List<string> _course_time_string = new List<string>();
       
        public List<string> course_time_string = new List<string>();//1-15:30-17:30
        public List<DoubleInt> course_time_int = new List<DoubleInt>();//11530-11730
        public List<string> course_sec_list
        {
            get
            {
                _course_time_string.Clear();
                foreach (string s in course_time_string)
                {
                    string[] tmp = s.Split('-');
                    int tmpt = int.Parse(tmp[1].Split(':')[0]);
                    if (tmpt < 12)
                        _course_time_string.Add(s.Substring(0, 1) + "1");
                    else if (tmpt < 18)
                        _course_time_string.Add(s.Substring(0, 1) + "2");
                    else
                        _course_time_string.Add(s.Substring(0, 1) + "3");
                }
                return _course_time_string;
            }
        }

        #region 时间参数
        private string[] am_time_sec = {"8:00","12:00"};
        private string[] pm_time_sec = {"12:00","18:00"};
        private string[] eve_time_sec = {"18:00","22:00"};
        #endregion
        #endregion

        #region 构造器
        
        public UCcurriculumWrite()
        {
            InitializeComponent();
            dataGrid1.CurrentCellChanged += new EventHandler(dataGrid1_CurrentCellChanged);
        }

        #endregion

        #region 事件

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            x_coor = this.dataGrid1.CurrentCell.ColumnNumber - 1;
            y_coor = this.dataGrid1.CurrentCell.RowNumber + 1;
            //1-5-15:30-17:30
            course_cell_data = x_coor + "-" + y_coor + "-" + GetCellText(x_coor, y_coor);
            tscombo_start.Text=string.Empty;
            tscombo_end.Text = string.Empty;
            #region 区间判断
            int timesec = this.dataGrid1.CurrentCell.RowNumber;
            if (timesec < 3)
            {
                tscombo_start.Text = am_time_sec[0];
                tscombo_end.Text = am_time_sec[1];
            }
            else if (timesec < 6)
            {
                tscombo_start.Text = pm_time_sec[0];
                tscombo_end.Text = pm_time_sec[1];
            }
            else if (timesec < 8)
            {
                tscombo_start.Text = eve_time_sec[0];
                tscombo_end.Text = eve_time_sec[1];
            }
            #endregion
        }

        private void tsb_ok_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tscombo_start.Text) ||
                string.IsNullOrEmpty(tscombo_end.Text))
                return;
            string tmp = GetCellText(x_coor, y_coor);
            if (!string.IsNullOrEmpty(GetCellText(x_coor, y_coor)))
                tsb_del_Click(new object(), new EventArgs());
            if(validateTimeData())
            {
                //显示到表格上
                this.courseTable.SetCellData(x_coor + 1, y_coor - 1, null,
                    tscombo_start.Text + "-" + tscombo_end.Text);
                //1-2-15:30-17:30
                //**修改意见**
                //**减少上午下午字段,即y坐标**
                //course_cell_data = x_coor + "-" + (y_coor/3+1) + "-" + tscombo_start.Text + "-" + tscombo_end.Text;
                course_cell_data = x_coor + "-" + tscombo_start.Text + "-" + tscombo_end.Text;
                //添加时间到本类变量course_time_string和course_time_int中
                this.course_time_string.Add(course_cell_data);
                this.course_time_int.Add(new DoubleInt
                    (CommonHelper.string2string11length(course_cell_data)));
                
            }
        }

        private void tsb_del_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(GetCellText(x_coor, y_coor))) return;
            //**修改;减少y字段冗余**
            //string tmp = x_coor + "-" + (y_coor / 3 + 1) + "-" + GetCellText(x_coor, y_coor).ToString();
            string tmp = x_coor + "-"+ GetCellText(x_coor, y_coor).ToString();
            DoubleInt tmp2 = new DoubleInt(
                CommonHelper.string2string11length(course_cell_data));
            course_time_string.Remove(tmp);
            course_time_int.RemoveAll(x => x==tmp2) ;
            DelCellData(x_coor, y_coor);
        }

        #endregion

        #region 方法

        //初始化表格
        public void initeDataGrid()
        {
            x_coor = 0;
            y_coor = 0;
            course_cell_data = null;
            course_time_string.Clear();
            course_time_int.Clear();
            this.courseTable.ClearCourseData();
        }

        //验证时间数据
        private bool validateTimeData()
        {
            string strRegex = @"^([0-9]|([0-1][0-9])|(2[0-3])):([0-9]|([0-5][0-9]))$";
            Regex re = new Regex(strRegex);
            if (!re.IsMatch(tscombo_start.Text) || !re.IsMatch(tscombo_end.Text))
            {
                MessageBox.Show("时间格式填写错误，请重新填写！", "错误");
                return false;
            }
            if (CommonHelper.string2Int3length(tscombo_end.Text)
                <= CommonHelper.string2Int3length(tscombo_start.Text))
            {
                MessageBox.Show("结束时间小于开始时间，请重新填写！", "错误");
                return false;
            }
            //1-15:30-17:30=>11530-11730
            string course_data = CommonHelper.string2string11length(x_coor + "-" 
                + tscombo_start.Text + "-" + tscombo_end.Text);
            foreach (DoubleInt di in course_time_int)
            {
                if (di.compare(course_data))
                {
                    MessageBox.Show("上课时间重合，请重新填写上课时间！");
                    return false;
                }
            }
            return true;
        }

        //设置标题
        public void setCaption(string caption_name)
        {
            courseTable.SetCaptionText(caption_name);
        }

        //获取表格文本
        public string GetCellText(int x_coord, int y_coord)
        {
            return courseTable.GetCellText(x_coord + 1, y_coord - 1);
        }

        //删除表格内容
        public void DelCellData(int x_coord, int y_coord)
        {
            courseTable.DelCellData(x_coord+1, y_coord-1);
        }

        //设置表格内容
        public void SetCellData(int day, int daySect, object data, string courseName)
        {
            string s = GetCellText(day, daySect);
            if (!string.IsNullOrEmpty(GetCellText(day, daySect )))
            {
                SetCellData(day, daySect + 1, data, courseName);
            }
            else
            {
                //**修改,**
                //courseTable.SetCellData(day + 1, daySect*3-3, data, courseName);
                //this.course_time_string.Add(day+"-"+daySect+"-"+courseName);
                //this.course_time_int.Add(new DoubleInt
                //    (CommonHelper.string2string11length(day + "-" + daySect + "-" + courseName)));
                courseTable.SetCellData(day + 1, daySect-1, data, courseName);
                this.course_time_string.Add(day + "-"+ courseName);
                this.course_time_int.Add(new DoubleInt
                    (CommonHelper.string2string11length(day + "-"  + courseName)));
            }
        }
        //public void SetCellData(int day, int daySect, int lessonNum, object data, string courseName)
        //{
        //    courseTable.SetCellData(day, daySect * 3 - 3, lessonNum, data, courseName);
        //    this.course_time_string.Add(day + "-" + daySect + "-" + courseName);
        //    this.course_time_int.Add(new DoubleInt
        //        (CommonHelper.string2string11length(day + "-" + daySect + "-" + courseName)));
        //}

        //隐藏时间设置栏
        public void FadeTimeSet()
        {
            this.toolStrip1.Visible = false;
        }

        #endregion
    }
}
